
const { default: mongoose } = require('mongoose')
const db = require('./db')
const bcryptjs = require('bcryptjs')
let cateSchema = new db.Schema({
    name: String,//分类名称
    state: Boolean,//状态
    time: String//创建时间
})

let cateModel = db.model('cate', cateSchema)


let userSchema = new db.Schema({
    username: {
        type: String,
    }, // 用户名
    password: {
        type: String,
        set(val) {   // 每次对数据库进行修改或插入时都会执行set
            return bcryptjs.hashSync(val) // 对入库的密码进行加密
        }
    }, // 密码
    petname: {
        type:String,
        default:'',
    },  //昵称
    picture: {
        type:String,
        default:""
    }, // 头像链接
    collect:{
        type:Array,
        default:[]
    } , // 收藏列表
    address: {
        type:Array,
        default:[]
    }, // 收货地址
    gender: {
        type:String,
        default:""
    }, // 性别
    wallet: {
        type:Object,
        default:function(){
            return {
                wallethandler:Array,
                remaining:Number
            }
        }
    }, // 钱包
    discount: {
        type:Array,
        default:[]
    }, // 优惠券
    phone: {
        type:String,
        default:""
    },  // 手机号
    paypassword: {
        type:String,
        default:""
    }, // 支付密码
    store:  {
        type:Array,
        default:[]
    }, //收藏店铺
    messageStore:  {
        type:Array,
        default:[]
    } //联系的商家
})


let userModel = db.model('user', userSchema, 'user')


// 收货地址
let addressListSchema = new db.Schema({
    userId: String,
    name: String, //收货人
    phone: String, //收货人手机号
    address: String,//收货地址
    isdefault: Boolean,//是否为默认地址
    latlong: Array//收货地址经纬度
})

let addressListModel = db.model("addressList", addressListSchema, 'addressList')

//订单页面
let orderSchema = new db.Schema({
    state: Number,//订单的状态，1未付款，2未发货，3未收货，4退换货
    goodsid: Array,//商品的id
    address:Object,//该订单的收货地址
    uid:String,//该用户订单id
    times:String //下单时间
})
let orderModel = db.model("order", orderSchema, 'order')

//商品页面
let flowerSchema = new db.Schema({
    name: String,//商品名称
    img: String,//商品图片
    time: String,//创建时间
    stock: Number,//库存 判断是否有货
    cate: {
        type: db.Types.ObjectId,
        ref: 'cate'
    },// 品种id
    store: {
        type: db.Types.ObjectId,
        ref: 'store'
    },//商家id
    //-- 详情页 --//
    imgs: Array,//详情页轮播图
    specs: Number,//规格 11/束 3/扎 尽量用吉利的数字
    price: Number,//商品单价 1/枝
    count: Number,//购买数量 0
    xiao: Number,//销量 1200笔
    Sincerely: String,//商品详情，
})
let flowerModel = db.model('flower', flowerSchema)


// 消息
const messageSchema = db.Schema({
    senderId: String,
    recipientId: String,
    content: String,
    time: Number,
    type: String,
})
const messageModel = db.model('message', messageSchema);

// 商家信息
let storeSchema = new db.Schema({
    name: String,//用户名
    src: String,//头像
    shop: String,//商店名称
    addr: String,//地址
    tel: Number,//电话
    email: String,//邮箱
    time: String,//创建时间
    account: Number,//登陆账号
    password: String,//密码
    latitude: Array// 经纬度
})
let storeModel = db.model('store', storeSchema)

const cartSchema = new mongoose.Schema({
    img:String,
    name:String,
    specs:String,
    price:Number,
    count:Number,
})

const cartModel = mongoose.model('carts',cartSchema)

module.exports = {
    cateModel,
    userModel,
    addressListModel,
    orderModel,
    flowerModel,
    messageModel,
    storeModel,
    cartModel
}